package com.itheima.web.response; /**
 * @author Xiao
 * @date 2021/8/30
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/resp01")
public class ResponseDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("demo01...");
        //重定向
        /*//1.设置响应状态码 302
        response.setStatus(302);
        //2. 设置响应头 Location
        response.setHeader("Location","/request_response/resp02");*/
        //重定向简化
        //1. 手动书写虚拟目录
        /*response.sendRedirect("/request_response/resp02");
        response.sendRedirect("https://www.baidu.com");*/
        //2.动态获取虚拟目录
        //String contextPath = request.getContextPath();
        //response.sendRedirect(contextPath + "/resp2");
        response.sendRedirect(request.getContextPath() + "/resp2");

        //web中的相对路径
        //request.getRequestDispatcher("./login.html").forward(request,response);
        //可以省略./
        request.getRequestDispatcher("login.html").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
